//
//  XQSmallCardController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit
import MJRefresh

class XQSmallCardController: XQBaseCardController,UITableViewDelegate,UITableViewDataSource {
    
    var refreshHandle: (() -> Void)?
    
    var applyHandle: (() -> Void)?
    
    var didSelectHandle: ((Int) -> Void)?
    
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.refreshHandle?()
        })
        
        return scrollView
    }()
    
    private lazy var mainView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var cardView : XQSmallCardView = {
        let v = XQSmallCardView()
        v.applyHandle = {[weak self] in
            guard let srongify = self else {return}
            srongify.applyHandle?()
        }
        return v
    }()
    
    private lazy var tableView : UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(XQSmallCardCell.self, forCellReuseIdentifier: "XQSmallCardCell")
        
        let headerView = XQSmallCardHeaderView()
        headerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 22)
        tableView.tableHeaderView = headerView
        
        let footerView = XQSmallCardFooterView()
        footerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 22)
        tableView.tableFooterView = footerView
        
        tableView.layer.cornerRadius = 16
        tableView.layer.masksToBounds = true
        
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .clear
        
        self.makeUI();
        self.makeConstraints()
    }
    
    func makeUI() {
        self.view.addSubview(self.scrollView)
        self.scrollView.addSubview(self.mainView)
        self.mainView.addSubview(self.cardView)
        self.mainView.addSubview(self.tableView)
    }
    
    func makeConstraints() {
        
        self.scrollView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-UIDevice.xq_bottomSafeMargin() - 48)
        }
        
        self.mainView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(UIDevice.xq_width())
        }
        
        self.cardView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(15)
            make.right.equalTo(-8)
            make.height.equalTo(266)
        }
        
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.cardView.snp.bottom).offset(16)
            make.height.equalTo(0)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(-16)
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listDataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XQSmallCardCell", for: indexPath) as! XQSmallCardCell
        cell.firstRow = indexPath.row == 0
        cell.lastRow = indexPath.row == self.tableView(tableView, numberOfRowsInSection: 0) - 1
        
        if (indexPath.row < self.listDataSource.count) {
            cell.model = self.listDataSource[indexPath.row]
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 || indexPath.row == self.tableView(tableView, numberOfRowsInSection: 0) - 1 {
            return 94
        }
        return 108
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath.row < self.listDataSource.count) {
            let model = self.listDataSource[indexPath.row]
            if let id = model.order {
                didSelectHandle?(id)
            }
        }
    }
    
    override func reloadData() {
        self.cardView.topCardInfo = self.topCardInfo
    }
    
    override func reloadDataSource() {
        self.tableView.reloadData()
        
        self.tableView.snp.updateConstraints { make in
            make.height.equalTo(self.listDataSource.count * 108 + 16)
        }
    }
    
    func endRefreshing() {
        self.scrollView.mj_header?.endRefreshing()
    }
}
